package com.mstore.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mstore.pojo.Shop;

/**
 * 
 * 分店信息
 * 
 */
public class ShopDao extends SuperDao {

	/**
	 * 
	 * 添加分店信息
	 */
	public void save(Shop shop) {
		super.save(shop);
	}

	/**
	 * 
	 * 删除分店信息
	 */
	public void delete(Shop shop) {
		/*
		 * String sql = "update b_shop set del_flag = 0 where id = " +
		 * shop.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update b_shop set del_flag = 0 where id = ");
		sb.append(shop.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 
	 * 更新分店信息
	 */
	public void update(Shop shop) {
		String separate = " set ";
		StringBuilder sql = new StringBuilder();
		sql.append("update ubcenter.b_shop");
		
		String address  = shop.getAddress();
		if (StringUtils.isNotBlank(address)) {
			sql.append(separate);
			sql.append("address = '");
			sql.append(address);
			sql.append("'");
			separate = ", ";
		}
		
		String name  = shop.getName();
		if (StringUtils.isNotBlank(name)) {
			sql.append(separate);
			sql.append("name = '");
			sql.append(name);
			sql.append("'");
			separate = ", ";
		}
		
		String type = shop.getType();
		if (StringUtils.isNotBlank(type)) {
			sql.append(separate);
			sql.append("type = '");
			sql.append(type);
			sql.append("'");
			separate = ", ";
		}
		
		sql.append(" where id = ");
		sql.append(shop.getId());
		
		super.executeUpdate(sql.toString());
	}

	/**
	 * 
	 * 获取某个分店的信息
	 */
	public Object getOneShop(Shop shop) {
		String sql = "from Shop where id=?";
		List<Object> objects = super.getObject(sql,
				new Object[] { shop.getId() });
		if (objects.size() > 0) {
			return objects.get(0);
		}
		else {
			return null;
		}
	}

	/**
	 * 
	 * 更新分店地址
	 */
	public void updateAddress(Shop shop) {
		/*
		 * String sql
		 * ="update b_shop set address='"+shop.getAddress()+"' where id= "
		 * +shop.getId(); super.executeUpdate(sql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("update b_shop set address='");
		sb.append(shop.getAddress());
		sb.append("' where id= ");
		sb.append(shop.getId());
		super.executeUpdate(sb.toString());
	}

	/**
	 * 
	 * 获取企业下的所有分店
	 */
	public List<Object> getAllShop(Shop shop) {
		String hql = "from Shop where eaId=? and delFlag = 1";
		return super.getObject(hql, new Object[] { shop.getEaId() });
	}

	/**
	 * 
	 * 对获取的所有分店记录，进行分页
	 */
	public List<Object> getShopPage(int index, Shop shop) {
		/*
		 * String hql = "from Shop where eaId="+shop.getEaId(); return
		 * super.findPage(index, hql);
		 */

		StringBuilder sb = new StringBuilder();
		sb.append("from Shop where eaId=");
		sb.append(shop.getEaId());
		return super.findPage(index, sb.toString());
	}

	/**
	 * 通过企业id、店铺名称查询店铺信息
	 */
	public List<Object> getShopByEaAndName(Long eaId, String shopName) {
		String hql = "from Shop where eaId=? and name = ? and delFlag = 1";
		return super.getObject(hql, new Object[] { eaId, shopName });
	}
}
